#pragma once
#include<iostream>
#include<map>

using namespace std;
#define MSIZE 1000000

#include<cstdlib>
#include<stdio.h>
#include<ctime>

void tTest_Map() {
	map<long, string> m;

	char buf[10];
	clock_t timeStart = clock();
	cout << "开始时间: " << timeStart << endl;

	for (int i = 0; i < MSIZE; i++) {
		try {
			snprintf(buf, 10, "%d", rand());
			//map 可以使用 [] 做 insertion
			//m.insert(pair<long, string>(i, buf));	//将key-value组合成一个pair
			m[i] = string(buf);
		}
		catch (exception& p) {
			cout << "i = " << i << " " << p.what() << endl;
			abort();
		}
	}

	cout << "map 赋值所需要时间: " << clock() - timeStart << endl;
	cout << "map 大小: " << m.size() << endl;
	cout << "map 最大大小: " << m.max_size() << endl;

	long target;
	cin >> target;

	//使用map的find进行查找
	timeStart = clock();
	map<long, string>::iterator pItem = m.find(target);
	cout << "map::find所需时间: " << clock() - timeStart << endl;

	//找到的结果迭代器中, first为key, second为value
	if (pItem != m.end())
		cout << "找到元素: " << (*pItem).first << ": " << (*pItem).second << endl;	//pItem->second	(类似智能指针)
	else
		cout << "找不到" << endl;
}